package ch.il07.converter.persistance.tests;

import static org.junit.Assert.fail;

import org.junit.Before;
import org.junit.Test;

import ch.il07.converter.persistence.Persistence;
import ch.il07.converter.persistence.PersistenceHelper;
import ch.il07.converter.persistence.entities.AccessLevel;
import ch.il07.converter.persistence.exceptions.PersistenceException;
import ch.il07.converter.persistence.exceptions.UserAlreadyExistsException;
import ch.il07.utils.RandomHelper;

public class AddUserTest {
	
	private Persistence persistence;

	@Before
	public void setUp() {
		try {
			persistence = PersistenceHelper.getPersistence();
		} catch (PersistenceException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void registerOk() {
		try {
			persistence.addUser(RandomHelper.randString(), RandomHelper.randString(), RandomHelper.randString(),
					AccessLevel.ADMINISTRATOR);
		} catch (UserAlreadyExistsException e) {
			fail(e.getMessage());
		} catch (PersistenceException e) {
			fail(e.getMessage());
		}
	}

	@Test
	public void doulbeAccount() {
		String username = RandomHelper.randString();
		String email = RandomHelper.randString();
		String pass = RandomHelper.randString();
		try {
			persistence.addUser(username, email, pass, AccessLevel.ADMINISTRATOR);
			persistence.addUser(username, email, pass, AccessLevel.ADMINISTRATOR);
			fail("should not be possible to add the same user twice");
		} catch (UserAlreadyExistsException e) {
			// expected
		} catch (PersistenceException e) {
			fail(e.getMessage());
		}
	}
}